﻿using IRepositoryDal;
using RepositoryEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryDal
{
    /// <summary>
    /// 部门数据库访问层
    /// </summary>
    public class DepartmentInfoDal : BaseDal<DepartmentInfo>, IDepartmentInfoDal
    {
        //数据库上文
        RepositoryDbContext _DbContext;

        public DepartmentInfoDal(RepositoryDbContext DbContext):base(DbContext)
        {
            _DbContext = DbContext;
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="departmentInfo"></param>
        /// <returns></returns>
        public bool AddDepartmentInfo(DepartmentInfo departmentInfo)
        {
            _DbContext.DepartmentInfo.Add(departmentInfo);
            int index = _DbContext.SaveChanges();
            if (index > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        ///// <summary>
        ///// 删除部门
        ///// </summary>
        ///// <returns></returns>
        //public bool DeleteDepartmentInfo(string departmentInfoId)
        //{
        //    //根据部门id查询要删除的部门对象
        //    DepartmentInfo departmentInfo = _DbContext.DepartmentInfo.FirstOrDefault(u => u.ID == departmentInfoId);
        //    if (departmentInfo == null)
        //    {
        //        return false;
        //    }
        //    else
        //    {
        //        _DbContext.DepartmentInfo.Remove(departmentInfo);
        //        int index = _DbContext.SaveChanges();
        //        if (index > 0)
        //        {
        //            return true;
        //        }
        //        else
        //        {
        //            return false;
        //        }
        //    }
        //}

        /// <summary>
        /// 根据id获取部门信息
        /// </summary>
        /// <param name="departmentInfoId">部门id</param>
        /// <returns></returns>
        public DepartmentInfo GetDepartmentInfo(string departmentInfoId)
        {
            DepartmentInfo departmentInfo = _DbContext.DepartmentInfo.FirstOrDefault(u => u.ID == departmentInfoId);
            return departmentInfo;
        }

        /// <summary>
        /// 获取部门集合
        /// </summary>
        /// <returns></returns>
        public List<DepartmentInfo> GetDepartmentInfos()
        {
            return _DbContext.DepartmentInfo.ToList();
        }

        /// <summary>
        /// 更新部门
        /// </summary>
        /// <param name="departmentInfo"></param>
        /// <returns></returns>
        public bool UpdateDepartmentInfo(DepartmentInfo departmentInfo)
        {
            DepartmentInfo entity = _DbContext.DepartmentInfo.FirstOrDefault(u=> u.ID == departmentInfo.ID);
            entity.DepartmentName = departmentInfo.DepartmentName;
            int index = _DbContext.SaveChanges();
            if (index > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
